/**************************************************************************
 *  创建者：        华磊
 *  开始时间：   2019.11.22
 *  copyright:  (C) 深圳市华友高科有限公司
 ***************************************************************************/
#include "weldstartinstructionview.h"
#include "ui_weldstartinstructionview.h"

WeldStartInstructionView::WeldStartInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WeldStartInstructionView)
{
    ui->setupUi(this);

    setGeometry(5,50,451,544);

    initialDisplayStyle(styleId);
    ui->radioButton_conditionIndex->setChecked(true);
    ui->radioButton_conditionValue->setChecked(false);

}

void WeldStartInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox->hide();
        ui->save_Btn->setGeometry(360,350,70,40);

        break;
    default:
        break;
    }
}

WeldStartInstructionView::~WeldStartInstructionView()
{
    delete ui;
}

void WeldStartInstructionView::getInput( WeldStartInstruction &instructionParaOut)
{
    instructionParaOut.comment=ui->note_lineEdit->text();
    instructionParaOut.weldPropertyIndex=ui->comboBox_weldProperty->currentIndex();
    if(ui->radioButton_conditionIndex->isChecked())
    {
        instructionParaOut.weldConditionIndex=ui->comboBox_conditionIndex->currentIndex();
        instructionParaOut.weldType=E_WELD_TYPE_COMMON;
    }
    else
    {
        instructionParaOut.weldVoltage=ui->lineEdit_weldVoltage->text().toInt();
        if(0==ui->comboBox_speedType->currentIndex())
        {
            instructionParaOut.weldAmp=ui->lineEdit_weldCurrent->text().toInt();
            instructionParaOut.weldType=E_WELD_TYPE_AMP;
        }
        else
        {
            instructionParaOut.weldSpeed=ui->lineEdit_weldCurrent->text().toInt();
            instructionParaOut.weldType=E_WELD_TYPE_SPEED;
        }
    }

}

void WeldStartInstructionView::updateView()
{

    ui->comboBox_weldProperty->setCurrentIndex(instructionPara.weldPropertyIndex);
    switch (instructionPara.weldType) {
    case E_WELD_TYPE_COMMON:
    ui->comboBox_conditionIndex->setCurrentIndex(instructionPara.weldConditionIndex);
    ui->radioButton_conditionIndex->setChecked(true);
    ui->radioButton_conditionValue->setChecked(false);
    break;
    case E_WELD_TYPE_AMP:
    ui->radioButton_conditionIndex->setChecked(false);
    ui->radioButton_conditionValue->setChecked(true);
    ui->lineEdit_weldVoltage->setText(QString::number(instructionPara.weldVoltage));
    ui->comboBox_speedType->setCurrentIndex(0);
    ui->lineEdit_weldCurrent->setText(QString::number(instructionPara.weldAmp));
    break;
    case E_WELD_TYPE_SPEED:
    ui->radioButton_conditionIndex->setChecked(false);
    ui->radioButton_conditionValue->setChecked(true);
    ui->lineEdit_weldVoltage->setText(QString::number(instructionPara.weldVoltage));
    ui->comboBox_speedType->setCurrentIndex(1);
    ui->lineEdit_weldCurrent->setText(QString::number(instructionPara.weldSpeed));
    break;
    default:
        break;
    }
    ui->note_lineEdit->setText(instructionPara.comment);
}

void WeldStartInstructionView::changeLanguage(int language)
{

}

void WeldStartInstructionView::finishEditInstruction(WeldStartInstruction instructionParaIn, QString &instructionStrOut)
{
    instructionStrOut.clear();
        instructionStrOut="Weld Start[";
        instructionStrOut+=QString::number(instructionParaIn.weldPropertyIndex)+",";
        switch (instructionParaIn.weldType) {
        case E_WELD_TYPE_COMMON:
            instructionStrOut+=QString::number(instructionParaIn.weldConditionIndex)+"]";
            break;
        case E_WELD_TYPE_AMP:
            instructionStrOut+=QString::number(instructionParaIn.weldVoltage)+"V,"
                    +QString::number(instructionParaIn.weldAmp)+"A]";
            break;
        case E_WELD_TYPE_SPEED:
            instructionStrOut+=QString::number(instructionParaIn.weldVoltage)+"V,"
                    +QString::number(instructionParaIn.weldSpeed)+"mm/sec]";
        default:
            break;
        }
        instructionStrOut+=";//"+instructionParaIn.comment;
}

void WeldStartInstructionView::on_button_editProperty_clicked()
{
    //todo
    emit showPropertyView();
}

void WeldStartInstructionView::on_button_editCondition_clicked()
{
    //todo
    emit showConditionView();
}

void WeldStartInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->setText("");
}

void WeldStartInstructionView::on_save_Btn_clicked()
{
    QString str1 = ui->lineEdit_weldVoltage->text();
    QString str2 = ui->lineEdit_weldCurrent->text();

    QString pattern("^[0-9]+$");
    QRegExp rxRoller(pattern);
    if(ui->radioButton_conditionValue->isChecked())
    {
        if(!rxRoller.exactMatch(str2)||!rxRoller.exactMatch(str1))
        {
           emit showErrorMsgBox(QString("存在非法字符，请修改！"));
           return;
        }
        else
        {
        getInput(instructionPara);
        finishEditInstruction(instructionPara,instructionStr);
    //    emit finishEditInstructionSignal();
        emit saveInstructionSignal(instructionStr);
        }
    }
    else
    {
    getInput(instructionPara);
    finishEditInstruction(instructionPara,instructionStr);
//    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionStr);
    }
}

void WeldStartInstructionView::on_radioButton_conditionIndex_clicked(bool checked)
{
    if(checked)
    {
        ui->radioButton_conditionIndex->setChecked(true);
        ui->radioButton_conditionValue->setChecked(false);
    }
    else
    {

    }
}

void WeldStartInstructionView::on_radioButton_conditionValue_clicked(bool checked)
{
    if(checked)
    {
        ui->radioButton_conditionIndex->setChecked(false);
        ui->radioButton_conditionValue->setChecked(true);
    }
    else
    {

    }
}
